\unitlength=8.5pt
\thinlines
\newcounter{xaxe}
\newcounter{yaxe}
\newcounter{Xaxe}
\newcounter{Yaxe}
\newcounter{Xaxetemp}
\newcounter{halfresult}
\newsavebox{\emptycell}
\newsavebox{\squarecell}
\newsavebox{\shadecell}
\newsavebox{\diamondcell}
\newsavebox{\superdiamondcell}
\newsavebox{\diamondsquarecell}
\newsavebox{\diamondshadecell}
\newsavebox{\superdiamondsquarecell}
\newsavebox{\superdiamondshadecell}
\sbox{\emptycell}{}
\sbox{\squarecell}{
\put(0,0){\line(1,0){1}}
\put(0,0){\line(0,1){1}}
\put(1,1){\line(-1,0){1}}
\put(1,1){\line(0,-1){1}}}
\sbox{\shadecell}{
\put(0,0){\usebox{\squarecell}}
\put(0,0.25){\line(1,0){1}}
\put(0,0.5){\line(1,0){1}}
\put(0,0.75){\line(1,0){1}}}
\sbox{\diamondcell}{\put(0.5,0.5){\circle*{0.4}}}
\sbox{\superdiamondcell}{\put(0.5,0.5){\circle{0.4}}}
\sbox{\diamondsquarecell}{
\put(0,0){\usebox\diamondcell}
\put(0,0){\usebox\squarecell}}
\sbox{\superdiamondsquarecell}{
\put(0,0){\usebox\superdiamondcell}
\put(0,0){\usebox\squarecell}}
\sbox{\diamondshadecell}{
\put(0,0){\usebox\diamondcell}
\put(0,0){\usebox\shadecell}}
\sbox{\superdiamondshadecell}{
\put(0,0){\usebox\superdiamondcell}
\put(0,0){\usebox\shadecell}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\halfcounter#1{\setcounter{halfresult}{0}\halfintcounter{#1}%
\addtocounter{#1}{\value{halfresult}}\addtocounter{#1}{\value{halfresult}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\halfintcounter#1{\ifnum\value{#1}>1\addtocounter{#1}{-2}%
\addtocounter{halfresult}{1}\halfintcounter{#1}\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\calculatepict#1{\setcounter{Xaxe}{0}\setcounter{Yaxe}{0}%
\calculateline'#1 e }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\calculateline#1 {%
\ifx e#1\else\ifx '#1\else\setcounter{Xaxetemp}{0}\calculatecell#1e%
\addtocounter{Yaxe}{1}%
\ifnum\value{Xaxe}<\value{Xaxetemp}\setcounter{Xaxe}{\value{Xaxetemp}}\fi%
\fi\expandafter\calculateline\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\calculatecell#1{%
\ifx e#1\else\ifx '#1\else\addtocounter{Xaxetemp}{1}%
\fi\expandafter\calculatecell\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\drawpict#1#2{%
\addtocounter{Xaxe}{1}\addtocounter{Yaxe}{3}%
\setcounter{xaxe}{0}\setcounter{yaxe}{\value{Yaxe}}%
\addtocounter{yaxe}{-2}\begin{picture}(\value{Xaxe},\value{Yaxe})%
\put(0,2){\vector(1,0){\value{Xaxe}}}%
\addtocounter{Yaxe}{-2}\put(0,2){\vector(0,1){\value{Yaxe}}}%
\put(\value{Xaxe},0){\llap{$B^{(i)}$}}%
\addtocounter{Yaxe}{1}\put(0.5,\value{Yaxe}){$i$}%
\halfcounter{Xaxe}\addtocounter{halfresult}{-2}%
\put(\value{halfresult},-1){\it#1}%
\drawline'#2 e \end{picture}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\drawline#1 {\ifx e#1\else%
\ifx '#1\else\drawcell#1e\setcounter{xaxe}{0}%
\addtocounter{yaxe}{-1}\fi\expandafter\drawline\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\drawcell#1{%
\ifx e#1\else%
\ifx '#1\else%
\ifx .#1\put(\value{xaxe},\value{yaxe}){\usebox{\emptycell}}\fi%
\ifx ;#1\put(\value{xaxe},\value{yaxe}){\usebox{\diamondcell}}\fi%
\ifx ?#1\put(\value{xaxe},\value{yaxe}){\usebox{\superdiamondcell}}\fi%
\ifx o#1\put(\value{xaxe},\value{yaxe}){\usebox{\squarecell}}\fi%
\ifx P#1\put(\value{xaxe},\value{yaxe}){\usebox{\diamondsquarecell}}\fi%
\ifx Q#1\put(\value{xaxe},\value{yaxe}){\usebox{\superdiamondsquarecell}}\fi%
\ifx x#1\put(\value{xaxe},\value{yaxe}){\usebox{\shadecell}}\fi%
\ifx X#1\put(\value{xaxe},\value{yaxe}){\usebox{\diamondshadecell}}\fi%
\ifx Z#1\put(\value{xaxe},\value{yaxe}){\usebox{\superdiamondshadecell}}\fi%
\addtocounter{xaxe}{1}\fi\expandafter\drawcell\fi}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\diagram#1#2{\calculatepict{#2}\drawpict{#1}{#2}}
